package com.woniuxy.lrz.carlproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.controller.form.levelform.UpdateSecondLevelForm;
import com.woniuxy.lrz.carlproject.dao.ServiceInfoMapper;
import com.woniuxy.lrz.carlproject.model.ServiceInfo;
import com.woniuxy.lrz.carlproject.model.ServiceSecondLevel;
import com.woniuxy.lrz.carlproject.dao.ServiceSecondLevelMapper;
import com.woniuxy.lrz.carlproject.service.ServiceInfoService;
import com.woniuxy.lrz.carlproject.service.ServiceSecondLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 二级分类服务表 服务实现类
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@Service
@Slf4j
public class ServiceSecondLevelServiceImpl extends ServiceImpl<ServiceSecondLevelMapper, ServiceSecondLevel> implements ServiceSecondLevelService {

     @Resource
     private ServiceSecondLevelMapper serviceSecondLevelMapper;

     @Resource
     private ServiceInfoMapper serviceInfoMapper;

    @Override
    public Page<ServiceSecondLevel> getAll(int current, int size) {
        Page<ServiceSecondLevel> page = serviceSecondLevelMapper.selectPage(new Page<>(current, size), null);
        if (page==null){
            throw  new AppException("查询二级分类服务失败");
        }
        return page;
    }

    @Override
    public void add(String name, Integer serviceFirstId) {
        QueryWrapper<ServiceSecondLevel> wrapper = new QueryWrapper<>();
      wrapper.eq("service_second_name",name);
        ServiceSecondLevel one = serviceSecondLevelMapper.selectOne(wrapper);
        if (one!=null){
            throw  new AppException("该服务名已经存在");
        }
        ServiceSecondLevel level = new ServiceSecondLevel();
        level.setServiceSecondName(name);
        level.setDeleted(0);
        level.setServiceFirstId(serviceFirstId);
        int insert = serviceSecondLevelMapper.insert(level);
        if (insert<1){
            throw  new AppException("添加失败");
        }
    }

    @Override
    public void delById(Integer id) {
        //拿二级服务id查服务详情表如果有则不允许删除
        QueryWrapper<ServiceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("service_second_id",id);
        List<ServiceInfo> serviceInfos = serviceInfoMapper.selectList(wrapper);
        if (serviceInfos.size()>0){
            throw  new AppException("该类下有关联服务删除失败");
        }else {
            UpdateWrapper<ServiceSecondLevel> wrapper1 = new UpdateWrapper<>();
            wrapper1.eq("service_second_id",id)
                    .set("deleted",1);
            int update = serviceSecondLevelMapper.update(null,wrapper1);
            if (update<1){
                throw  new AppException("逻辑删除二级分类失败");
            }
        }
    }

    @Override
    public void updateSecondLevel(UpdateSecondLevelForm updateSecondLevelForm) {
        QueryWrapper<ServiceSecondLevel> wrapper = new QueryWrapper<>();
        wrapper.eq("service_second_name",updateSecondLevelForm.getServiceSecondName());
        ServiceSecondLevel one = serviceSecondLevelMapper.selectOne(wrapper);
        if (one!=null){
            throw  new AppException("该服务名已经存在");
        }
        UpdateWrapper<ServiceSecondLevel> wrapper1 = new UpdateWrapper<>();
        wrapper1.eq("service_second_id",updateSecondLevelForm.getServiceSecondId())
                .set("service_second_name",updateSecondLevelForm.getServiceSecondName())
                .set("service_first_id",updateSecondLevelForm.getServiceFirstId());
        int update = serviceSecondLevelMapper.update(null, wrapper1);
        if (update<1){
            throw  new AppException("修改二级服务失败");
        }
    }

    //根据一级服务id查看二级服务（分页）
    public Page<ServiceSecondLevel> getSecondLevelByFirstId(int serviceFirstId, int current, int size) {
        QueryWrapper<ServiceSecondLevel> wrapper = new QueryWrapper<>();
        wrapper.eq("service_first_id",serviceFirstId);
        Page<ServiceSecondLevel> page = serviceSecondLevelMapper.selectPage(new Page<>(current, size), wrapper);
        if (page==null){
            throw  new AppException("查询二级分类服务失败");
        }
        return page;
    }
}
